#ifndef DEPENDENCYSORTER_H
#define DEPENDENCYSORTER_H

#include <stack>
#include <map>
#include <utility>
#include <vector>
#include "Common\stdafx.h"
#include "Expression.h"

namespace CExpression
{
	struct StringLessFunc
	{
		bool operator()(const ExpressionString *const left,const ExpressionString *const right)
		{
			return *left<*right;
		}
	};

	class CEXPRESSIONUTIL_API DependencySorter
	{
	public:
		DependencySorter():errorOccured(false),errMsg(){}
		std::vector< std::pair<const ExpressionString*,Expression*> >* Sort(std::map<ExpressionString,Expression*> &exps);
		bool ErrorOccured(){return this->errorOccured;}
		ExpressionString ErrorMsg(){return this->errMsg;}
	private:
		void SortCore(
			std::map<ExpressionString,Expression*> &exps,
			const ExpressionString &expName,
			std::stack<const ExpressionString*> &stack,
			std::map<const ExpressionString*,const ExpressionString*,StringLessFunc> &parentSymbol);
		bool errorOccured;
		ExpressionString errMsg;
	};
}

#endif